package com.guigu.qingcheng.servicegoods.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guigu.qingcheng.pojo.goods.Goods;
import com.guigu.qingcheng.pojo.goods.TbSpu;
import com.guigu.qingcheng.servicegoods.service.TbSpuService;
import com.guigu.qingcheng.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-07-08
 */
@RestController
@RequestMapping("/spu")
public class TbSpuController {
    @Autowired
    TbSpuService spuService;

    //查询所有
    @GetMapping("findAll")
    public R findAll(){
        List<TbSpu> list = this.spuService.list(null);
        if (list.size()>0)
            return R.ok().data("list",list);
        return R.error().data("list",list);
    }

    //分页不带条件
    @GetMapping("findPage")
    public R findPage(@RequestParam("page") int page, @RequestParam("size") @PathVariable int size){
        //创建page对象
        Page<TbSpu> pageTbSpu = new Page<>(page,size);
        this.spuService.page(pageTbSpu,null);

        long total = pageTbSpu.getTotal();//总记录数
        List<TbSpu> records = pageTbSpu.getRecords(); //数据list集合
        if (total>0 && records.size()>0)
            return R.ok().data("total",total).data("rows",records);
        return R.error();
    }

    //条件查询不带分页
    @PostMapping("findList")
    public R findList(@RequestBody Map<String,Object> searchMap){
        List<TbSpu> list = spuService.findList(searchMap);
        if (list.size()>0){
            return R.ok().data("list",list);
        }
        return R.error().data("list",list);
    }

    //条件查询带分页
    @PostMapping("findPage")
    public R pageTbPrefCondition(@RequestBody Map<String, Object> searchMap, @RequestParam("page") int page, @RequestParam("size") int size){
        Page<TbSpu> pageTbSpu = spuService.findPage(searchMap,page,size);
        if (pageTbSpu.getRecords().size()>0){
            return R.ok().data("total",pageTbSpu.getTotal()).data("rows",pageTbSpu.getRecords());
        }
        return R.error().data("total",pageTbSpu.getTotal()).data("rows",pageTbSpu.getRecords());
    }

    //根据id查询
    @GetMapping("findById")
    public R findById(@RequestParam("id") String id){
        TbSpu tbSpu = new TbSpu();
        tbSpu = spuService.getById(id);
        return R.ok().data("spu",tbSpu);
    }

    //增加数据
    @PostMapping("add")
    public R addTbSpu(@RequestBody TbSpu tbSpu){
        boolean b = spuService.save(tbSpu);
        if (b)
            return R.ok();
        return R.error();
    }
    //修改数据
    @PostMapping("update")
    public R updateTbSpu(@RequestBody TbSpu tbSpu){
        boolean flag = this.spuService.updateById(tbSpu);
        if (flag)
            return R.ok();
        return R.error();
    }

    //删除数据
    @GetMapping("delete")
    public R deleteTbSpuById(@RequestParam("id") String id){
        boolean flag = spuService.removeById(id);
        if (flag)
            return R.ok();
        return R.error();
    }


    @PostMapping("/save")
    public R save(@RequestBody Goods goods){
        this.spuService.saveGoods(goods);
        return R.ok();
    }

    @GetMapping("findGoodsById")
    public R findGoodsById(String id){
        Goods goods = spuService.findGoodsById(id);
        return R.ok().data("goods",goods);
    }

    @GetMapping("findGoodsBySpuId")
    public Goods findGoodsBySpuId(String spuId){
        Goods goods = spuService.findGoodsById(spuId);
        return goods;
    }

    @PostMapping("audit")
    public R audit(@RequestParam String id, @RequestParam String status){
        spuService.audit(id,status);
        return R.ok();
    }

    @GetMapping("pull")
    public R pull(String id){
        spuService.pull(id);
        return R.ok();
    }

    @GetMapping("put")
    public R put(String id){
        spuService.put(id);
        return R.ok();
    }

    @GetMapping("putMany")
    public R putMany(String[] ids){
        int i = spuService.putMany(ids);
        return R.ok().data("0","上架"+i+"个商品");
    }

    // 逻辑删除讲师的方法
    @DeleteMapping("removeMany")
    public R removeMany(@RequestParam String id) {
        boolean flag = spuService.removeById(id);
        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    //商品回收站显示数据
    @GetMapping("backStation")
    public R backStation(@RequestParam String id){
        List<TbSpu> list = this.spuService.backStation(id);
        return R.ok().data("spuList",list);
    }

    //回收商品
    @GetMapping("backGoods")
    public R backGoods(@RequestParam String id){
        Boolean flag = this.spuService.backGoods(id);
        if (flag)
        return R.ok().data("flag",true);
        return R.error().data("flag",false);
    }

    //逻辑删除商品
    @GetMapping("ljdelete")
    public R ljdelete(@RequestParam("id") String id){
        Boolean flag = this.spuService.ljdelete(id);
        if (flag)
            return R.ok().data("flag",true);
        return R.error().data("flag",false);
    }

    @PostMapping("findPageSaudit")
    public R findPageSaudit(@RequestBody Map<String, Object> searchMap, @RequestParam("page") Integer page, @RequestParam("size") Integer size){
        Page<TbSpu> pageTbSpu = spuService.findPageSaudit(searchMap,page,size);
        if (pageTbSpu.getRecords().size()>0){
            return R.ok().data("total",pageTbSpu.getTotal()).data("rows",pageTbSpu.getRecords());
        }
        return R.error().data("total",pageTbSpu.getTotal()).data("rows",pageTbSpu.getRecords());
    }

    @PostMapping("findBackPage")
    public R findBackPage(@RequestBody Map<String, Object> searchMap, @RequestParam("page") Integer page, @RequestParam("size") Integer size){
        Page<TbSpu> pageTbSpu = spuService.findBackPage(searchMap,page,size);
        if (pageTbSpu.getRecords().size()>0){
            return R.ok().data("total",pageTbSpu.getTotal()).data("rows",pageTbSpu.getRecords());
        }
        return R.error().data("total",pageTbSpu.getTotal()).data("rows",pageTbSpu.getRecords());
    }
}

